package cc.ycws.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import cc.ycws.core.domain.Gunfail;
import cc.ycws.core.domain.Unitdoor;
import cc.ycws.core.domain.Uploadfile;
import cc.ycws.core.domain.Visitors;
import cc.ycws.core.service.GunfailService;
import cc.ycws.core.service.UnitdoorService;
import cc.ycws.core.service.UploadService;
import cc.ycws.core.service.VisitorsService;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ssm/v2")
public class GunApi {
    @Resource
    UnitdoorService unitdoorService;
    @Resource
    UploadService uploadService;
    @Resource
    VisitorsService visitorsService;
    @Resource
    GunfailService gunfailService;

    @RequestMapping(value = "/wwluserinfo", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String wwluserinfo(@RequestBody Map json, ModelMap map) {
        //当前系统时间为结束时间
        Date data = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stoptime = dateFormat.format(data);


        String starttime = (String) json.get("starttime");
        String idnumber = (String) json.get("idnumber");


        Unitdoor unitdoor = unitdoorService.selectIdnumber(idnumber);
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("starttime", starttime);
        maps.put("stoptime", stoptime);
        maps.put("yaddress", unitdoor.getAddress());
        List<Uploadfile> list = uploadService.selectTimeAddress(maps);
        List<Visitors> list1 = visitorsService.selectTimeAndAddress(maps);
        List<Uploadfile> listfail = uploadService.gunfail("0");
        List<Visitors> listfail1 = visitorsService.gunfail("1");
        map.addAttribute("list", list);
        map.addAttribute("list1", list1);
        map.addAttribute("listfail", listfail);
        map.addAttribute("listfail1", listfail1);
        map.addAttribute("systemtime", stoptime);
        JSONObject json1 = new JSONObject(map);
        gunfailService.delete(idnumber);
        return json1.toString();
    }

    @RequestMapping(value = "/wwlfail", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String wwlfail(@RequestBody Map json, Gunfail gunfail) {
        String id = (String) json.get("id");
        String gun = (String) json.get("gun");
        String usertype = (String) json.get("usertype");
        int id1 = Integer.parseInt(id);
        gunfail.setId(id1);
        gunfail.setGun(gun);
        gunfail.setUsertype(usertype);
        int num = gunfailService.insert(gunfail);
        if (num == 1) {
            return "{state" + ":1}";    //成功
        } else {
            return "{state" + ":0}";    //失败
        }
    }
}
